﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Drawing;
using Microsoft.DirectX;
using NGE;

namespace MH
{
    [Serializable]
    public enum SpriteType
    {
        PC = 0,
        OPC = 1,
        NPC = 2,
        Monster = 3,
        Box = 10,
        Door = 11,
    }
    [Serializable]
    public enum DoorState
    {
        Closed = 0,
        Locked = 1,
        Open = 2,
    }
    [Serializable]
    public enum BoxState
    {
        Closed = 0,
        Locked = 1,
        Open = 2,
    }
    [Serializable]
    public class Sprite
    {
        public Sprite()
        {
            Name = "non_name";            
            Avatar = new NImage();
            //Size = new Size(1, 1);
        }
        public string Name;

        public NImage Avatar;
        public Size AvatarSize;

        public bool IsHide;

        public Vector3 Location;
        public Vector3 LastLocation;
        public Vector3 TargetLocation;

        public Direction Direction;

        [NonSerialized]
        protected int needMoveStep = 0;
        [NonSerialized]
        protected float needMovePixel = 0f;
        
        public float MoveSpeedPixel = 1f;

        public SpriteType Type;
        //public void GetAvatar()
        //{
        //    Avatar = null;
        //    Avatar = new NImage();
        //    Avatar.GetData(AvatarFileName);
        //}
        public int GetAvatarFrame(Size tileSize)
        {
            int val = 0;
            if (Direction == Direction.U || Direction == Direction.D)
            {
                val = tileSize.Height;
            }
            else
            {
                val = tileSize.Width;

            }
            float nmp = needMovePixel;
            nmp %= val;
            if (nmp > val / 4 * 3)
            {
                return 1;
            }
            if (nmp > val / 4 * 2)
            {
                return 2;
            }
            if (nmp > val / 4 * 1)
            {
                return 3;
            }
            if (nmp > val / 4 * 0)
            {
                return 4;
            }
            return 0;
        }
        public Vector3 GetAvatarLocation(Size tileSize)
        {
            Vector3 loc = Location;
            loc.X *= tileSize.Width;
            loc.Y *= tileSize.Height;
            return loc;
        }

        public float NeedMove
        {
            get
            {
                return needMovePixel;
            }
        }

        public Vector3 GetNextLocation(Size tileSize)
        {
            Vector3 v3 = Location;
            if (needMovePixel > 0)
            {
                v3 = LastLocation;
            }
            if (Direction == Direction.U)
            {
                v3.Y -= tileSize.Height;
            }
            else if (Direction == Direction.L)
            {
                v3.X -= tileSize.Width;
            }
            else if (Direction == Direction.D)
            {
                v3.Y += tileSize.Height;
            }
            else if (Direction == Direction.R)
            {
                v3.X += tileSize.Width;
            }
            return v3;
        }
        public Vector3 GetNextLocation(Vector3 loc,Direction dir, Size tileSize)
        {
            Vector3 v3 = loc;
            if (dir == Direction.U)
            {
                v3.Y -= tileSize.Height;
            }
            else if (dir == Direction.L)
            {
                v3.X -= tileSize.Width;
            }
            else if (dir == Direction.D)
            {
                v3.Y += tileSize.Height;
            }
            else if (dir == Direction.R)
            {
                v3.X += tileSize.Width;
            }
            return v3;
        }
        public Vector3 GetNextNextLocation(Size tileSize)
        {
            Vector3 v3 = GetNextLocation(tileSize);
            if (needMovePixel > 0)
            {
                v3 = LastLocation;
            }
            if (Direction == Direction.U)
            {
                v3.Y -= tileSize.Height;
            }
            else if (Direction == Direction.L)
            {
                v3.X -= tileSize.Width;
            }
            else if (Direction == Direction.D)
            {
                v3.Y += tileSize.Height;
            }
            else if (Direction == Direction.R)
            {
                v3.X += tileSize.Width;
            }
            return v3;
        }
        public void JumpTo(Vector3 loc, Size tileSize)
        {
            Location = new Vector3(loc.X * tileSize.Width, loc.Y * tileSize.Height, 0);
        }
        //public Vector3 GetTileLocation(Size tileSize)
        //{ 
        //}


        //public void Move(Direction dir, int steps, Size tileSize, Scene scn)
        //{
        //    if (needMovePixel == 0)
        //    {
        //        Direction = dir;

        //        if (scn.TileCodeLayer[GetNextLocation(tileSize)] != null)
        //        {
        //            if (!scn.TileCodeLayer[GetNextLocation(tileSize)].CHR)
        //            {
        //                return;
        //            }
        //        }
        //        if (scn.NPCs != null)
        //        {
        //            foreach (NonPlayerCharacter npc in scn.NPCs)
        //            {
        //                if (GetNextLocation(tileSize).Equals(npc.Location))
        //                {
        //                    return;
        //                }
        //            }
        //        }

        //        needMoveStep = steps;
        //        if (Direction == Direction.U || Direction == Direction.D)
        //        {
        //            {
        //                needMovePixel = tileSize.Height * needMoveStep;
        //            }
        //        }
        //        else if (Direction == Direction.L || Direction == Direction.R)
        //        {
        //            {
        //                needMovePixel = tileSize.Width * needMoveStep;
        //            }
        //        }
        //        LastLocation = Location;

        //        scn.Move(Direction, steps, moveSpeed);
        //    }
        //}
    }
}
